package domain.model;

import domain.SpelerTekenAfbeelding;
import domain.SpelerTekenChar;
import domain.iSpelerTeken;

public class Board {

	private iSpelerTeken[][] board;
	private int[] winnendePosities = new int[6];

	public Board(int height, int width, iSpelerTeken spelerteken) {
		if (spelerteken instanceof SpelerTekenChar){
			board = new SpelerTekenChar[height][width];
		}else if (spelerteken instanceof SpelerTekenAfbeelding){
			board = new SpelerTekenAfbeelding[height][width];
		}else{
			board = new iSpelerTeken[height][width];
		}
		
	}

	public iSpelerTeken get(int row, int col) {
		return board[row][col];
	}

	public void set(int row, int col, iSpelerTeken iSpelerTeken) {
		board[row][col] = iSpelerTeken;
	}

	public int getWidth() {
		return board[0].length;
	}

	public int getHeight() {
		return board.length;
	}

	public boolean isFull() {
		boolean full = false;
		int row = 0;
		while (row < board.length && !full) {
			full = (isRowFull(row));
			row++;
		}
		return full;
	}

	private boolean isRowFull(int row) {
		boolean full = true;
		int col = 0;
		while (col < board[0].length && full) {
			full = board[row][col] != null;
			col++;
		}
		return full;
	}
	
	public iSpelerTeken[][] getBoard() {
		return board;
	}

	// other useful methods

	public iSpelerTeken[][] toArray() {
		return board;
	}


	private boolean isColumnFull(int col) {
		boolean full = true;
		int row = 0;
		while (row < board[0].length && full) {
			full = board[row][col] != null;
			row++;
		}
		return full;
	}
	
	private boolean isDiagonal1Full() {
		boolean full = true;
		int col = 0;
		while (col < board[0].length && full) {
			full = board[col][col] != null;
			col++;
		}
		return full;
	}
	
	private boolean isDiagonal2Full() {
		boolean full = true;
		int row = 0;
		int col = 2;
		while (row < board[0].length && full) {
			full = board[row][col-row] != null;
			row++;
		}
		return full;
	}
	
	//public
	
	public int[] getWinnendePosities(){
		for (int i =0; i < board.length; i++){
			if (isRowFull(i)){
				if (board[i][0].getTeken() == board[i][1].getTeken() && board[i][0].getTeken() == board[i][2].getTeken()){
					winnendePosities[0] = i;
					winnendePosities[1] = 0;
					winnendePosities[2] = i;
					winnendePosities[3] = 1;
					winnendePosities[4] = i;
					winnendePosities[5] = 2;
					return winnendePosities;
				}
			}
			if (isColumnFull(i)){
				if (board[0][i].getTeken() == board[1][i].getTeken() && board[0][i].getTeken() == board[2][i].getTeken()){
					winnendePosities[0] = 0;
					winnendePosities[1] = i;
					winnendePosities[2] = 1;
					winnendePosities[3] = i;
					winnendePosities[4] = 2;
					winnendePosities[5] = i;
					return winnendePosities;
				}
			}
		}
		if (isDiagonal1Full()){
			if (board[0][0].getTeken() == board[1][1].getTeken() && board[1][1].getTeken() == board[2][2].getTeken()){
				winnendePosities[0] = 0;
				winnendePosities[1] = 0;
				winnendePosities[2] = 1;
				winnendePosities[3] = 1;
				winnendePosities[4] = 2;
				winnendePosities[5] = 2;
				return winnendePosities;
			}
		}
		if (isDiagonal2Full()){
			if (board[0][2].getTeken() == board[1][1].getTeken() && board[1][1].getTeken() == board[2][0].getTeken()){
				winnendePosities[0] = 0;
				winnendePosities[1] = 2;
				winnendePosities[2] = 1;
				winnendePosities[3] = 1;
				winnendePosities[4] = 2;
				winnendePosities[5] = 0;
				return winnendePosities;
			}
		}
		
		return null;
	}

	@Override
	public String toString() {
		String result = getWidth() + " " + getHeight();
		for (int i = 0; i < getWidth(); i++) {
			for (int j = 0; j < getHeight(); j++) {
				result += " " + board[i][j];
			}
		}
		return result;
	}

}
